from fastapi import FastAPI

from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html
from mldong.auth_middleware import AuthMiddleware

from modules.sys.controllers.auth_controller import router as auth_router
from modules.sys.controllers.user_controller import router as user_router
from modules.sys.controllers.role_controller import router as role_router
from modules.sys.controllers.menu_controller import router as menu_router
from modules.sys.controllers.post_controller import router as post_router
from modules.sys.controllers.dept_controller import router as dept_router
from modules.sys.controllers.config_controller import router as config_router
from modules.sys.controllers.dict_controller import router as dict_router
from modules.sys.controllers.dict_item_controller import router as dict_item_router
from modules.sys.controllers.file_info_controller import router as file_info_router

app = FastAPI(docs_url=None, redoc_url=None)
app.include_router(auth_router)
app.include_router(user_router)
app.include_router(role_router)
app.include_router(menu_router)
app.include_router(post_router)
app.include_router(dept_router)
app.include_router(config_router)
app.include_router(dict_router)
app.include_router(dict_item_router)
app.include_router(file_info_router)
app.add_middleware(AuthMiddleware, while_list=[], token_key="Authorization")

# 自定义Swagger UI路由，使用本地资源
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url="/openapi.json",
        title=app.title + " - Swagger UI",
        swagger_js_url="/static/swagger-ui-bundle.js",
        swagger_css_url="/static/swagger-ui.css",
        swagger_favicon_url="/static/favicon.ico",
    )

app.mount("/static", StaticFiles(directory="static"), name="static")